<?php
  require(dirname(__FILE__).'/global.php');
  require(ROOT.'/admin/isLogin.php');
  
  $submit = $_POST['submit'];
  if(isset($submit))
  {
      $username = $_POST['username'];
      $useremail = $_POST['useremail'];
      $userpass = $_POST['userpass'];
      $userpass2 = $_POST['userpass2'];
      $usergroup = $_POST['usergroup'];
      if(empty($username)||empty($userpass)||empty($usergroup)||empty($useremail)){
          $smarty->assign('tip','请填完所有项');
      }
      else if(strlen($username)<2 || strlen($username) > 10 || !preg_match('/^[a-zA-Z0-9_]{1,}$/',$username)){
          $smarty->assign('tip','用户名格式输入错误');
      }
      else if(strlen($userpass)<6 || strlen($userpass) > 16){
          $smarty->assign('tip','密码格式输入错误'); 
      }
      else if($userpass != $userpass2){
          $smarty->assign('tip','用户密码与确认密码不一致'); 
      }
      else if(!DataConverter::isEmail($useremail)){
          $smarty->assign('tip','邮箱格式错误'); 
      }
      else
      {
          require(ROOT.'/include/encrypt.class.php');
          $userid = DataConverter::initSerialNo();
          $userpass = Encrypt::encrypt($userpass);
          $userimg = 'static/images/default.gif';
          $usertime = DateUtil::get_YY_mm_dd_HH_mm_ss();
          $userip = $_SERVER['REMOTE_ADDR'];
          $sql = "insert into talking_user(UserID,UserName,UserPass,UserEmail,UserImg,UserGroup,UserRegistTime,UserRegistIP) values('$userid','$username','$userpass','$useremail','$userimg','$usergroup','$usertime','$userip')";
          $db->execute($sql);
          $smarty->assign('tip','添加成功');   
      }
  } 
  $smarty->display('user_add.html');
  $db->close();                                
?>
